<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane v-loading="loading" label="角色管理" name="role">
            <el-button type="primary" size="mini" @click="showDialog = true">+ 新增角色</el-button>
            <el-table :data="list" style="margin:20px 0">
              <el-table-column label="序号" type="index" :index="indexMethod" width="150" />
              <el-table-column label="角色名称" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row}">
                  <el-button type="text">分配权限</el-button>
                  <el-button type="text">编辑</el-button>
                  <el-button type="text" @click="del(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- @size-change="handleSizeChange"
            @current-change="handleCurrentChange" -->
            <el-pagination
              :current-page="page"
              :page-sizes="[1, 2, 3, 4,5]"
              :page-size="pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="company">公司内容</el-tab-pane>
        </el-tabs>
      </el-card>

      <el-dialog title="新增角色" :visible="showDialog" @close="closeDialog">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入角色名称" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="form.description" placeholder="请输入角色描述" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button type="primary" @click="clickSubmit">确认</el-button>
          <el-button @click="closeDialog">取消</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { reqGetRoleList, reqDelRole, reqAddRole } from '@/api/setting'
export default {
  name: 'Setting',
  data() {
    return {
      activeName: 'role',
      list: [],
      page: 1,
      pagesize: 2,
      total: 0,
      loading: false,
      showDialog: false,
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: ['blur', 'change'] }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    clickSubmit() {
      this.$refs.form.validate(async(valid) => {
        if (!valid) return
        console.log('可以发请求了')
        const res = await reqAddRole(this.form)
        console.log(res)
        this.showDialog = false
        this.$message.success('新增角色成功')
        this.getRoleList()
      })
    },
    closeDialog() {
      this.showDialog = false
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    del(id) {
      this.$confirm('该操作不可逆,确定要删除么?', '温馨提示').then(async() => {
        const res = await reqDelRole(id)
        console.log(res)
        this.$message.success('删除角色成功')
        if (this.list.length === 1 && this.page > 1) {
          this.page--
        }
        this.getRoleList()
      }).catch(() => {
        this.$message.error('取消删除')
      })
    },
    indexMethod(index) {
      return index + 1 + this.pagesize * (this.page - 1)
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.page = 1
      this.getRoleList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page = val
      this.getRoleList()
    },
    async getRoleList() {
      this.loading = true
      const { data: { rows, total }} = await reqGetRoleList(this.page, this.pagesize)
      this.list = rows
      this.total = total
      this.loading = false
    }
  }
}
</script>

<style>

</style>
